<template>
  <a-modal
    title="编辑商品"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="商品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['goodsName', {initialValue: goods.goodsName}]" />
            </a-form-item>
          </a-col>
<!--          <a-col :md="12" :sm="24">-->
<!--            <a-form-item-->
<!--              label="商品描述"-->
<!--              :labelCol="labelCol"-->
<!--              :wrapperCol="wrapperCol"-->
<!--            >-->
<!--              <a-input v-decorator="['goodsDesc', {initialValue: goods.goodsDesc}]" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :md="12" :sm="24">-->
<!--&lt;!&ndash;            <a-form-item&ndash;&gt;-->
<!--&lt;!&ndash;              label="用户id"&ndash;&gt;-->
<!--&lt;!&ndash;              :labelCol="labelCol"&ndash;&gt;-->
<!--&lt;!&ndash;              :wrapperCol="wrapperCol"&ndash;&gt;-->
<!--&lt;!&ndash;            >&ndash;&gt;-->

<!--&lt;!&ndash;              &ndash;&gt;-->
<!--&lt;!&ndash;            </a-form-item>&ndash;&gt;-->
<!--          </a-col>-->
        </a-row>
        <a-row>
          <a-col :md="20" :sm="24">
            <a-form-item
              label="发布时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-date-picker
                :disabledDate="disableTime"
                v-decorator="['showTime', {initialValue: goods.showTime}]"
                @ok="getDataValue"
                @change="getDataValue"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="20">
          <a-col :md="20" :sm="24">
            <a-form-item
              label="商品描述"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-textarea cols="48" rows="12"  v-decorator="['goodsDesc', {initialValue: goods.goodsDesc}]"/>
<!--              <a-input v-decorator="['goodsDesc', {initialValue: goods.goodsDesc}]" />-->
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { editGoods } from '@/api/basic/goodsList'
import moment from "moment";

export default {
  name: 'EditGoods',
  props: {
  },
  data () {
    return {
      expandedKeys: [1],
      treeData: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      goods: {},
      form: this.$form.createForm(this),
      showTime: ''
    }
  },
  created () {
  },
  methods: {
    edit (record) {
      this.visible = true
      this.goods = record
      this.goods.listId = record.listId
      console.log(111)
      console.log(this.goods)
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.showTime = this.showTime+' 00:00:00'
          editGoods(Object.assign(this.goods, { 'goodsName': values.goodsName, 'goodsDesc': values.goodsDesc, 'showTime': values.showTime  }))
            .then(res => {
              console.log("输出编辑")
              console.log(this.goods)
              if (res.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    },
    getDataValue (value) {
      this.showTime = moment(value).format('YYYY-MM-DD') // 转换
    },
    disableTime(current){
      return current && current < moment().startOf('day')
    }
  }
}
</script>
